/*
 * @Author: szx
 * @Date: 2021-10-29 13:44:25
 * @LastEditTime: 2021-10-29 14:16:53
 * @Description:
 * @FilePath: \leetcode\335\335.cpp
 */
#include <iostream>
#include <vector>
using namespace std;

class Solution {
   public:
    bool isSelfCrossing(vector<int> &distance) {
        vector<vector<int>> v{{0, 0}};
        int x = 0, y = 0;
        for (int i = 0; i < distance.size(); i++) {
            switch (i % 4) {
                case 0:
                    y += distance[i];
                    break;
                case 1:
                    x -= distance[i];
                    break;
                case 2:
                    y -= distance[i];
                    break;
                case 3:
                    x += distance[i];
                    break;
                default:
                    break;
            }
            v.push_back({x, y});
        }
        // for (auto &i : v) {
        //     for (int &j : i) {
        //         cout << j << " ";
        //     }
        //     cout << endl;
        // }
        for (int i = 1; i < v.size(); i += 4) {
            for (int j = i + 3; j < v.size(); j += 2) {
                vector<int> min_vertical, max_vevertical, min_horizon,
                    max_horizon;
                // if (v[i - 1][0] > v[i][0]) {
                //     min_vertical = v[i];
                //     max_vevertical = v[i - 1];
                // } else {
                //     min_vertical = v[i - 1];
                //     max_vevertical = v[i];
                // }

                if (v[i][0] <= v[j][0] && v[i][0] >= v[j - 1][0] &&
                    v[j][1] <= v[i][1] && v[j][1] >= v[i - 1][1]) {
                    return true;
                }
            }
        }
        for (int i = 3; i < v.size(); i += 4) {
            for (int j = i + 3; j < v.size(); j += 2) {
                vector<int> min_vertical, max_vevertical, min_horizon,
                    max_horizon;
                if (v[i][0] >= v[j][0] && v[i][0] <= v[j - 1][0] &&
                    v[j][1] >= v[i][1] && v[j][1] <= v[i - 1][1]) {
                    return true;
                }
            }
        }
        return false;
    }
};

int main() {
    Solution s;
    vector<int> v = {1,1,2,1,1};
    cout << s.isSelfCrossing(v) << endl;
    return 0;
}